package com.eedi.framework.permission.dal.mapper;

import cn.hutool.core.util.StrUtil;
import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.enums.UserTypeEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.mybatis.core.dataobject.BaseDO;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.framework.permission.controller.sys.vo.role.SysRolePageReq;
import com.eedi.framework.permission.controller.sys.vo.role.SysRoleSimpleResp;
import com.eedi.framework.permission.convert.SysRoleConvert;
import com.eedi.framework.permission.dal.dataobject.SysRoleDO;
import com.eedi.framework.permission.enums.SysRoleCodeEnum;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.lang.Nullable;

import java.util.Collection;
import java.util.List;

@Mapper
public interface SysRoleMapper extends BaseMapperX<SysRoleDO> {

    default PageResult<SysRoleDO> selectPage(SysRolePageReq reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SysRoleDO>()
                .likeIfPresent(SysRoleDO::getSysRoleName, reqVO.getSysRoleName())
                .likeIfPresent(SysRoleDO::getSysRoleCode, reqVO.getSysRoleCode())
                .eqIfPresent(SysRoleDO::getSysRoleStatus, reqVO.getSysRoleStatus())
                .betweenIfPresent(BaseDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(SysRoleDO::getId));
    }

    default SysRoleDO selectByName(String name, UserTypeEnum systemUserType) {
        LambdaQueryWrapperX<SysRoleDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(SysRoleDO::getSysRoleName, name);
        return selectOne(queryWrapper);
    }

    default SysRoleDO selectByCode(String code, UserTypeEnum systemUserType) {
        LambdaQueryWrapperX<SysRoleDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(SysRoleDO::getSysRoleCode, code);
        return selectOne(queryWrapper);
    }

    default List<SysRoleDO> selectListByStatus(@Nullable Collection<CommonStatusEnum> statuses) {
        LambdaQueryWrapperX<SysRoleDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.inIfPresent(SysRoleDO::getSysRoleStatus, statuses);
        return selectList(queryWrapper);
    }

    default List<SysRoleSimpleResp> selectByRoleIds(Collection<String> roleIds){
        List<SysRoleDO> sysRoleDOS = selectBatchIds(roleIds);
       return sysRoleDOS.stream().map(SysRoleConvert.INSTANCE::convert).toList();
    }

    default List<SysRoleSimpleResp> getByCode(String code){
        if (StrUtil.isBlank(code)){
            return List.of();
        }
        List<SysRoleDO> sysRoles = selectList(SysRoleDO::getSysRoleCode, code);
        return sysRoles.stream().map(SysRoleConvert.INSTANCE::convert).toList();
    }

    default List<SysRoleSimpleResp> selectSysRoleCode(SysRoleCodeEnum sysRoleCode){
        List<SysRoleDO> sysRoles = selectList(SysRoleDO::getSysRoleCode, sysRoleCode.getCode());
        return sysRoles.stream().map(SysRoleConvert.INSTANCE::convert).toList();
    }

}
